package com.guli.seckill.scheduled;

import com.guli.seckill.constant.SeckillConstant;
import com.guli.seckill.service.SeckillService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author zzb
 * @Date 2024/4/24 14:52
 * @Version 1.0
 */
@Service
@Slf4j
public class SeckillSkuScheduled {
    @Autowired
    SeckillService seckillService;
    @Autowired
    RedissonClient redissonClient;

    /**
     * 定时上架三天后秒杀的商品
     */
    @XxlJob(value = "seckillSku3Days")
    public void uploadSeckillSku3Days() {
        RLock lock = redissonClient.getLock(SeckillConstant.UPLOAD_LOCK);
        log.info("执行上传秒杀商品......");
        try {
            lock.tryLock(30, TimeUnit.SECONDS);
            seckillService.uploadSeckillSku3Days();
        } catch (InterruptedException e) {
            log.info(e.getMessage());
        } finally {
            lock.unlock();
        }
    }
}
